package com.sqsf.util;

import com.sqsf.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectAll() {
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUsernameAndPassword() {
        for (User user : mapper.selectByUsernameAndPassword("a1", "a2")) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUsername() {
        for (User user : mapper.selectByUsername("a1")) {
            System.out.println(user);
        }
    }

    @Test
    public void deleteByUid() {
        mapper.deleteByUid(55);
    }

    @Test
    public void updatePasswordByUid() {
        mapper.updatePasswordByUid(55, "a");
    }

    @Test
    public void insertUser() {
        User u = new User("a1", "a2", "a3");
        mapper.insertUser(u);
    }

    @Test
    public void selectUser() {
        for (User user : mapper.selectUser()) {
            System.out.println(user);
        }
    }

    @Test
    public void selectUser1() {
        for (User user : mapper.selectUser1()) {
            System.out.println(user);
        }
    }

    @Test
    public void selectUser_Role() {
        for (User user : mapper.selectUser_Role()) {
            System.out.println(user);
        }
    }

    @Test
    public void deleteByArray() {
        int[] array = {5, 6, 7, 8, 9};
        mapper.deleteByArray(array);
    }
}